package com.beehoo.dcportal.admin.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.ThemeDao;
import com.beehoo.dcportal.admin.service.ThemeService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.WriteFile;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.FileBean;
import com.beehoo.dcportal.model.ThemeBean;
import com.beehoo.dcportal.model.ThemeCodeBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class ThemeServiceImpl extends BaseService implements ThemeService {

	private ThemeDao themeDao;

	public void setThemeDao(ThemeDao themeDao) {
		this.themeDao = themeDao;
	}

	@Override
	public String createTheme(ThemeBean themeBean) throws BaseException {
		validateThemeBean(themeBean);
		themeBean = disposeUploadFile(themeBean);
		String themeId = themeDao.createTheme(themeBean);
		if (CommonUtils.nvlToString(themeId).equals("")) {
			throw new BaseException("custom.messages.error.theme.create.error");
		}
		return themeId;
	}

	@Override
	public void delTheme(String streetId, String themeId) throws BaseException {
		ThemeBean themeBean = this.queryTheme(streetId, themeId);
		if (themeBean != null) {
			String thInuseString = themeBean.getThInuse();
			if (thInuseString.equals("1")) {
				throw new BaseException(
						"custom.messages.error.theme.in.use.error");
			}
		}
		int ret = themeDao.delTheme(themeId);
		if (ret <= 0) {
			throw new BaseException("custom.messages.error.theme.del.error");
		}
	}

	@Override
	public ThemeBean queryTheme(String streetId, String themeId)
			throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if (CommonUtils.nvlToString(themeId).equals("")) {
			throw new BaseException("custom.messages.error.theme.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("themeId", themeId);
		return themeDao.queryTheme(paramMap);
	}

	@Override
	public List<ThemeBean> queryThemes(String streetId) throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		return themeDao.queryThemes(paramMap);
	}

	@Override
	public void setInUseTheme(String streetId, String themeId)
			throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if (CommonUtils.nvlToString(themeId).equals("")) {
			throw new BaseException("custom.messages.error.theme.id.empty");
		}

		Map<String, Object> paramMap1 = new HashMap<String, Object>();
		paramMap1.put("streetId", streetId);
		paramMap1.put("thInuse", "0");

		Map<String, Object> paramMap2 = new HashMap<String, Object>();
		paramMap2.put("streetId", streetId);
		paramMap2.put("themeId", themeId);
		paramMap2.put("thInuse", "1");

		themeDao.setInUseTheme(paramMap1, paramMap2);
	}

	@Override
	public void updateTheme(ThemeBean themeBean) throws BaseException {
		validateThemeBean(themeBean);
		themeBean = disposeUploadFile(themeBean);
		int ret = themeDao.updateTheme(themeBean);
		if (ret <= 0) {
			throw new BaseException("custom.messages.error.theme.update.error");
		}
	}

	@Override
	public List<ThemeCodeBean> queryThemeCode() throws BaseException {
		return themeDao.queryThemeCode();
	}

	private void validateThemeBean(ThemeBean themeBean) throws BaseException {
		if (themeBean == null) {
			throw new BaseException("custom.messages.error.theme.create.error");
		}
		if (CommonUtils.nvlToString(themeBean.getThName()).equals("")) {
			throw new BaseException("custom.messages.error.theme.name.empty");
		}
	}

	private ThemeBean disposeUploadFile(ThemeBean themeBean) throws BaseException {
		FileBean logoFileBean = themeBean.getLogoFile();
		FileBean bgFileBean = themeBean.getBgFile();

		if (logoFileBean != null) {
			int fileSize = logoFileBean.getFileSize();
			if(fileSize > Constant.UPLOAD_FILE_MAX_SIZE){
				throw new BaseException("struts.messages.error.file.too.large");
			}
			if(!CommonUtils.validateImageFileType(logoFileBean.getContentType())){
				throw new BaseException("struts.messages.error.content.type.not.allowed");
			}
			String fileName = CommonUtils.getUploadFileName(themeBean.getStreetId(), Constant.IMG_PORTAL_LOGO);
			themeBean.setThLogo(themeBean.getLogoImgDir() + fileName);

			File target = new File(themeBean.getLogoImgPath() + fileName);
			WriteFile wf = new WriteFile(logoFileBean.getFile(), target, fileSize);
			try {
				wf.write();
			} catch (BaseException e) {
				throw new BaseException("custom.messages.error.theme.upload.logo.error");
			}
		}
		
		if (bgFileBean != null) {
			int fileSize = bgFileBean.getFileSize();
			if(fileSize > Constant.UPLOAD_FILE_MAX_SIZE){
				throw new BaseException("struts.messages.error.file.too.large");
			}
			if(!CommonUtils.validateImageFileType(bgFileBean.getContentType())){
				throw new BaseException("struts.messages.error.content.type.not.allowed");
			}
			String fileName = CommonUtils.getUploadFileName(themeBean.getStreetId(), Constant.IMG_PORTAL_BG);
			themeBean.setThBgImg(themeBean.getBgImgDir() + fileName);

			File target = new File(themeBean.getBgImgPath() + fileName);
			WriteFile wf = new WriteFile(bgFileBean.getFile(), target, fileSize);
			try {
				wf.write();
			} catch (BaseException e) {
				throw new BaseException("custom.messages.error.theme.upload.back.error");
			}
		}
		return themeBean;
	}

}
